package com.antler.luajavalibrary.luaext.utils;

import android.graphics.Color;
import android.util.Log;

public class ColorUtils {
    protected static final String TAG = "antler_" + ColorUtils.class.getSimpleName();

    public static int parseColor(String colorString) {
        if (colorString.charAt(0) == '#') {
            // Use a long to avoid rollovers on #ffXXXXXX
            long color = Long.parseLong(colorString.substring(1), 16);
            if (colorString.length() == 7) {
                // Set the alpha value
                color |= 0x00000000ff000000;
            } else if (colorString.length() == 4) {
                long r = color & 0x0000000000000f00;
                long g = color & 0x00000000000000f0;
                long b = color & 0x000000000000000f;
                color = ((r << 12) | (r << 8) | (g << 8) | (g << 4) | (b << 4) | (b)) & 0xFFFFFF;
                color |= 0x00000000ff000000;
                Log.v(TAG, String.format("0x%x", color));
            } else if (colorString.length() != 9) {
                throw new IllegalArgumentException("Unknown color");
            }
            return (int) color;
        }
        return Color.parseColor(colorString);
    }
}
